import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class AddOEQuestionGUI extends JPanel implements ActionListener, FocusListener{

	

/*

(BorderLayout CENTER)

Row1:	Type the question here

Row2:	What type of response?

(BorderLayout SOUTH)

Row3:	Cancel and Submit Buttons

*/

	

	//private JFrame mainFrame;

	private JPanel main, row1, row2, row3;

	private JTextField question;

	private JButton cancelButton, submitButton;

	private JLabel	responseType;

	private JComboBox comboBox;

	

	public AddOEQuestionGUI(){

		//super("Add an Open-Ended Question");

		this.setLayout(new BorderLayout());

		//mainFrame = new JFrame("Add an Open-Ended Question");

		main = new JPanel();

		main.setLayout(new BoxLayout(main,BoxLayout.Y_AXIS));

		row1 = new JPanel(new FlowLayout());

		row2 = new JPanel(new FlowLayout());

		row3 = new JPanel(new FlowLayout());

		

		question = new JTextField("Type your question here...");

		question.setPreferredSize(new Dimension(285,20));

		responseType = new JLabel("What type of response would you like?");

		comboBox = new JComboBox(new String[] {"Short Answer","Essay"});

		comboBox.setSelectedIndex(0);

		cancelButton = new JButton("Cancel");

		submitButton = new JButton("Submit");

		

		row1.add(question);

		row2.add(responseType);

		row2.add(comboBox);

		row3.add(cancelButton);

		row3.add(submitButton);

		main.add(row1);

		main.add(row2);

				

		// add action listeners to each of the buttons

		question.addFocusListener(this);

		cancelButton.addActionListener(this);

	    submitButton.addActionListener(this);



	    this.add(main, BorderLayout.NORTH);

	    this.add(row3, BorderLayout.SOUTH);

		//mainFrame.add(main, BorderLayout.NORTH);

		//mainFrame.add(row3, BorderLayout.SOUTH);

		//mainFrame.setSize(500,500);

		//mainFrame.setVisible(true);

	}



	public void focusGained(FocusEvent e){

		if(e.getSource()==question){

			if(question.getText().equals("Type your question here...")){

				question.selectAll();

			}

		}

	}

	

	public void focusLost(FocusEvent e){}

	

	public void actionPerformed( ActionEvent event){
		GUIMaster gm = GUIMaster.getInstance();
		SystemController sc = gm.getWorkhorse();

		//Code for handling actions

		if(event.getSource()==cancelButton){

			gm.questionAddCancelled();

			//Go to the last page--probably the page where a survey preview can be seen

			

		}else if(event.getSource()==submitButton){

			if(question.getText().equals("") || question.getText().equals("Type your question here...")){

				JOptionPane.showMessageDialog(this, "Please enter a question.", "Warning!", JOptionPane.WARNING_MESSAGE);
			}
			else{
				Question q = new Question(question.getText(), 3, sc.currentCid);
				sc.addSurveyQuestion(q);
				gm.questionAddSuccessful();
			}
		}

	}



	public static void main(String args[]){

		JFrame frame = new JFrame();

		frame.add(new AddOEQuestionGUI());

		//AddOEQuestionGUI application = new AddOEQuestionGUI();

        frame.setSize(500,500);

        frame.setVisible(true);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}